//
//  GetPhoto.m
//  PolyCarCollect
//
//  Created by iMac on 16/7/9.
//  Copyright (c) 2016年 谷旭. All rights reserved.
//

#import "GetPhoto.h"
#import "ShangChuanTouXiangView.h"
@interface GetPhoto ()

@property(nonatomic,strong)NSMutableArray *selectAsssts;
@property(nonatomic,strong)NSMutableArray *selectPhotos;
@property (nonatomic, copy)void (^finishBlock)(UIImage* curImage,NSData * imageData);
@property (nonatomic, copy)void (^finishBlock1)(NSMutableArray *selectPhotos,NSMutableArray *selectAssets,NSMutableArray *selectImageDatas);
@property(nonatomic,strong)UIViewController * curController;
@end

@implementation GetPhoto

+ (GetPhoto *)sharedInstance
{
    static GetPhoto *sharedGetPhotoInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedGetPhotoInstance = [[self alloc] init];
    });
    return sharedGetPhotoInstance;
}
+(void)getPhoto:(UIViewController *)controller completion:(void (^)(UIImage *, NSData *))completion
{
    [[self sharedInstance] getPhotoWithControler:controller completion:completion];
}

- (void)getPhoto:(UIViewController *)controller andTitle:(NSString *)title completion:(void (^)(UIImage* curImage,NSData * imageData))completion{
    
    self.curController = controller;
    self.finishBlock = completion;
    
    ShangChuanTouXiangView *chuan = [ShangChuanTouXiangView view];
    chuan.titleLabel.text = title;
    chuan.frame = CGRectMake(0, 0, MainScreenWidth, 140+ScreenBottom);
    chuan.manageClickBlock = ^(NSInteger index) {
        [self.curController dismissViewControllerAnimated:YES completion:nil];
        if (index == 102) {
            //            拍摄
            [self takePhoto];
            
        }else if (index == 103) {
            //            手机相册选择
            [self LocalPhoto];
        }
    };
    [ZZPUtils showActionSheetWithView:chuan andController:self.curController];
    
}



+(void)getPhotos:(UIViewController *)controller AndSelectAssets:(NSMutableArray *)selectAssets completion:(void (^)(NSMutableArray* selectPhotos,NSMutableArray *selectAssets,NSMutableArray * selectImageDatas))completion{
    GetPhoto *photoManager=[[GetPhoto alloc]init];
    [photoManager getPhotos:controller AndSelectAssets:selectAssets completion:completion];
    
}

-(void)getPhotoWithControler:(UIViewController *)controller completion:(void (^)(UIImage *, NSData *))completion
{
    self.curController = controller;
    self.finishBlock = completion;
    [self getCameaPicture];
}
-(void)getPhotos:(UIViewController *)controller AndSelectAssets:(NSMutableArray *)selectAssets completion:(void (^)(NSMutableArray* selectPhotos,NSMutableArray *selectAssets,NSMutableArray * selectImageDatas))completion{
    
    self.curController = controller;
    self.finishBlock1=completion;
    _selectAsssts=selectAssets;
    [self pushTzPicker];
    
}

#pragma mark 相册
- (void)getCameaPicture
{
    SPAlertController *alertController = [SPAlertController alertControllerWithTitle:nil message:nil preferredStyle:SPAlertControllerStyleActionSheet];
    SPAlertAction *action1 = [SPAlertAction actionWithTitle:@"拍摄" style:SPAlertActionStyleDefault handler:^(SPAlertAction * _Nonnull action) {
         //拍照
         [self takePhoto];
    }];
    [alertController addAction:action1];
    action1.titleColor = mainFontColor;
    action1.titleFont = [UIFont systemFontOfSize:17];
    SPAlertAction *action2 = [SPAlertAction actionWithTitle:@"从相册选择" style:SPAlertActionStyleDefault handler:^(SPAlertAction * _Nonnull action) {
        //相册
        [self LocalPhoto];
    }];
    [alertController addAction:action2];
    action2.titleColor = mainFontColor;
    action2.titleFont = [UIFont systemFontOfSize:17];
    SPAlertAction *cancel = [SPAlertAction actionWithTitle:@"取消" style:SPAlertActionStyleCancel handler:^(SPAlertAction * _Nonnull action) {
    }];
    [alertController addAction:cancel];
    cancel.titleColor = mainFontColor;
    cancel.titleFont = [UIFont systemFontOfSize:17];
    [self.curController presentViewController:alertController animated:YES completion:nil];
    
//    ShangChuanTouXiangView *chuan = [ShangChuanTouXiangView view];
//       chuan.frame = CGRectMake(0, 0, MainScreenWidth, 140+ScreenBottom);
//       chuan.manageClickBlock = ^(NSInteger index) {
//           [self.curController dismissViewControllerAnimated:YES completion:nil];
//           if (index == 102) {
//               //            拍摄
//                [self takePhoto];
//
//           }else if (index == 103) {
//               //            手机相册选择
//               [self LocalPhoto];
//           }
//       };
//       [ZZPUtils showActionSheetWithView:chuan andController:self.curController];
    

}

//开始拍照
- (void)takePhoto
{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//        [[picker navigationBar] setBarTintColor:[UIColor whiteColor]];
        picker.delegate = self;
        picker.allowsEditing = NO;
        picker.sourceType = sourceType;
        [self.curController presentViewController:picker animated:YES completion:^{
            self.curController.navigationController.navigationBar.barStyle = UIBarStyleDefault;

        }];
    }else
    {
        NSLog(@"模拟其中无法打开照相机,请在真机中使用");
    }
}

//打开本地相册
- (void)LocalPhoto
{
    
    TZImagePickerController *imagePickerVc=[[TZImagePickerController alloc] initWithMaxImagesCount:1 columnNumber:4 delegate:nil pushPhotoPickerVc:YES];
    
    
    imagePickerVc.oKButtonTitleColorNormal = rgba(24, 27, 37, 1);
    imagePickerVc.naviBgColor = [UIColor whiteColor];
    imagePickerVc.naviTitleColor = rgba(24, 27, 37, 1);
    imagePickerVc.barItemTextColor = rgba(24, 27, 37, 1);
    
//    imagePickerVc.selectedAssets=_selectAsssts;
    imagePickerVc.selectedAssets=nil;
    //不允许 选择 视频和gif 动图 如需要 自行设置
    imagePickerVc.allowPickingVideo=NO;
    imagePickerVc.allowPickingGif=NO;
    imagePickerVc.allowPreview=NO;
   
    imagePickerVc.allowCrop = YES;
    
   
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        _selectAsssts=[NSMutableArray arrayWithArray:assets];//选中手机图片资源信息数组，包含位置等信息 < PHAsset or ALAsset对象 ，TZImagePickerController 选中后提供>
        _selectPhotos=[NSMutableArray arrayWithArray:photos];//选中图片数组<UIImage对象>
        NSMutableArray *datas=[NSMutableArray array];
        for (UIImage *img in _selectPhotos) {
            [datas addObject:UIImageJPEGRepresentation(img, 0.5)];
        }

        if (self.finishBlock) {
            self.finishBlock(photos.firstObject,datas.firstObject);
        }
    }];
    imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
    [_curController presentViewController:imagePickerVc animated:YES completion:nil];
    
    
//    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//    //更改背景颜色
//    [[picker navigationBar] setBarTintColor:[UIColor whiteColor]];
//
//    [picker navigationBar].barStyle = UIBarStyleBlack;
//    [picker navigationBar].translucent = NO;
//    [picker navigationBar].tintColor = [UIColor whiteColor];
//    picker.view.backgroundColor = [UIColor whiteColor];
//    picker.navigationController.delegate = self;
//
////    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"导航栏"] forBarMetrics:UIBarMetricsDefault];
//
//    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//    picker.delegate = self;
//    picker.allowsEditing = NO;
//    picker.modalPresentationStyle = UIModalPresentationFullScreen;
//    [self.curController presentViewController:picker animated:YES completion:^{
//        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
//        //更改背景颜色
//    }];
}

//- (void)navigationController:(UINavigationController *)navigationController
//  willShowViewController:(UIViewController *)viewController
//                animated:(BOOL)animated {
//    //更改背景颜色
//    [navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
//
//    navigationController.navigationBar.barStyle = UIBarStyleBlack;
//    navigationController.navigationBar.translucent = NO;
//    navigationController.navigationBar.tintColor = [UIColor whiteColor];
//    navigationController.navigationBar.backgroundColor = mainColor;
//    [navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"导航栏"] forBarMetrics:UIBarMetricsDefault];
//}



- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated

{

    // bug fixes: UIIMagePickerController使用中偷换StatusBar颜色的问题

    if ([navigationController isKindOfClass:[UIImagePickerController class]]) {

        [navigationController.navigationBar setBarStyle:UIBarStyleBlack];

        navigationController.navigationBar.translucent = NO;

         //设置成想要的背景颜色

        [navigationController.navigationBar setBarTintColor:mainColor];

    }

}


- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"])
    {
        UIImage * image;
        if (picker.allowsEditing) {
            image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
        }else
        {
            image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        }
        NSData * imageData = UIImageJPEGRepresentation(image, 0.05);
        if (self.finishBlock) {
            self.finishBlock(image,imageData);
        }
        [picker dismissViewControllerAnimated:YES completion:^{
            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];

        }];
    }
}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{

    [picker dismissViewControllerAnimated:YES completion:^{
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

        
    }];
}

//选取多图
-(void)pushTzPicker{
    //最多选择 多少张 可修改 也可传入
    TZImagePickerController *imagePickerVc=[[TZImagePickerController alloc] initWithMaxImagesCount:9 columnNumber:4 delegate:nil pushPhotoPickerVc:YES];
    
    imagePickerVc.selectedAssets=_selectAsssts;
    //不允许 选择 视频和gif 动图 如需要 自行设置
    imagePickerVc.allowPickingVideo=NO;
    imagePickerVc.allowPickingGif=NO;
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

        self.selectAsssts=[NSMutableArray arrayWithArray:assets];//选中手机图片资源信息数组，包含位置等信息 < PHAsset or ALAsset对象 ，TZImagePickerController 选中后提供>
        self.selectPhotos=[NSMutableArray arrayWithArray:photos];//选中图片数组<UIImage对象>
        NSMutableArray *datas=[NSMutableArray array];
        for (UIImage *img in self.selectPhotos) {
            [datas addObject:UIImageJPEGRepresentation(img, 0.05)];
        }
        
        if(self.finishBlock1){
            
          self.finishBlock1(self.selectPhotos,self.selectAsssts,datas);
        }
    }];
    
    [self.curController presentViewController:imagePickerVc animated:YES completion:^{
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];

    }];

    
}

+(void)getPhotos:(UIViewController *)controller AndSelectAssets:(NSMutableArray *)selectAssets andNum:(NSInteger)num completion:(void (^)(NSMutableArray* selectPhotos,NSMutableArray *selectAssets,NSMutableArray * selectImageDatas))completion{
    GetPhoto *photoManager=[[GetPhoto alloc]init];
    [photoManager getPhotos:controller AndSelectAssets:selectAssets andPhotoNum:num completion:completion];
    
}
-(void)getPhotos:(UIViewController *)controller AndSelectAssets:(NSMutableArray *)selectAssets andPhotoNum:(NSInteger)num completion:(void (^)(NSMutableArray* selectPhotos,NSMutableArray *selectAssets,NSMutableArray * selectImageDatas))completion{
    
    _curController = controller;
    _finishBlock1=completion;
    _selectAsssts=selectAssets;
    [self pushTzPickerWithNum:num];
    
}

//选取多图
-(void)pushTzPickerWithNum:(NSInteger)num{
    //最多选择 多少张 可修改 也可传入
    TZImagePickerController *imagePickerVc=[[TZImagePickerController alloc] initWithMaxImagesCount:num columnNumber:4 delegate:nil pushPhotoPickerVc:YES];
    
    imagePickerVc.selectedAssets=_selectAsssts;
    //不允许 选择 视频和gif 动图 如需要 自行设置
    imagePickerVc.allowPickingVideo=NO;
    imagePickerVc.allowPickingGif=NO;
    
    imagePickerVc.naviBgColor = mainColor;
    
//    imagePickerVc.allowCrop = YES;
    
   
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        _selectAsssts=[NSMutableArray arrayWithArray:assets];//选中手机图片资源信息数组，包含位置等信息 < PHAsset or ALAsset对象 ，TZImagePickerController 选中后提供>
        _selectPhotos=[NSMutableArray arrayWithArray:photos];//选中图片数组<UIImage对象>
        NSMutableArray *datas=[NSMutableArray array];
        for (UIImage *img in _selectPhotos) {
            [datas addObject:UIImageJPEGRepresentation(img, 0.05)];
        }
        
        if(_finishBlock1){
          _finishBlock1(_selectPhotos,_selectAsssts,datas);
        }
    }];
    imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
    [_curController presentViewController:imagePickerVc animated:YES completion:nil];

    
}



@end
